package com.herohead.misc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import redis.clients.jedis.Jedis;

import com.herohead.redis.GetRedisServer;
import com.herohead.redis.RedisVars;

//import sun.net.www.URLConnection;

//import com.herohead.notifications.Numerous;


public class Numerous {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		// TODO Auto-generated method stub


		String user = "nmrs_ORRVw8jZDHAP:";

		byte[] encodedBytes = Base64.encodeBase64(user.getBytes());
		String userAuth =  new String(encodedBytes);

		GetRedisServer grs = new GetRedisServer();
		RedisVars.redisHost = grs.getRedisHost();
		//System.out.println(RedisVars.redisHost);
		Jedis jd = new Jedis(RedisVars.redisHost);

		try {
			if (!grs.checkForRedis(jd)) {
				System.out.println("redis server not found");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Set<String> key = jd.hkeys("h_prod:numerousapi:keys");

		for (String s : key) {
			System.out.println(s);
		}

		String stuff = jd.hget("h_prod:numerousapi:keys", "NetworkerCloneInQue");

		JSONObject jo = getJsonObject(stuff);

		System.out.println(jo.get("metricID"));
		System.out.println(jo.get("AUTHCODE"));

		//	apiKey = getConfig.getValueJson(strKey, "apiKey");
		//	apiSecret = getConfig.getValueJson(strKey, "apiSecret");

		System.exit(0);

		updateValue(userAuth,"2088622069300404688","250");
		updateDescription(userAuth,"2088622069300404688","Yo Yo Yo");


		/**
		try {
			String webPage = "https://api.numerousapp.com/v1/users/2448141602705064388/metrics";


			URL url = new URL(webPage);
			HttpsURLConnection HttpsURLConnection = (HttpsURLConnection) url.openConnection();
			HttpsURLConnection.setRequestProperty("Authorization", "Basic bm1yc19PUlJWdzhqWkRIQVA6");
			HttpsURLConnection.setRequestMethod("GET");
			InputStream is = HttpsURLConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);

			int numCharsRead;
			char[] charArray = new char[1024];
			StringBuffer sb = new StringBuffer();
			while ((numCharsRead = isr.read(charArray)) > 0) {
				sb.append(charArray, 0, numCharsRead);
			}
			String result = sb.toString();

			System.out.println("*** BEGIN ***");
			System.out.println(result);
			System.out.println("*** END ***");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}


		try {

			//String url = "https://api.numerousapp.com/v1/metrics/2088622069300404688/events";
			String url = "https://api.numerousapp.com/v1/metrics/2088622069300404688";
			//String url = "https://api.numerousapp.com/v1/users/2088622069300404688/metrics";
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
			con.setRequestProperty("Authorization", "Basic bm1yc19PUlJWdzhqWkRIQVA6");
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestMethod("PUT");

			con.setDoOutput(true);


			JSONObject jobj = new JSONObject();
			jobj.put("value", "10");
			jobj.put("description", "cool it works - again");
			jobj.put("units", "kgs");

			System.out.println(jobj.toJSONString());
			//System.out.println(jobj.toString());

			//System.exit(0);

			String data = jobj.toJSONString();

			//String data =  "{\"value\":\"99\"}";
			OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
			out.write(data);
			out.close();

			// new InputStreamReader(con.getInputStream());

			BufferedReader in = new BufferedReader(
					new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			System.out.println(response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

		 **/


	}


	public static void updateValue (String owner, String appkey,String value) {
		try {

			String url = "https://api.numerousapp.com/v1/metrics/" + appkey + "/events";
			//String url = "https://api.numerousapp.com/v1/metrics/2088622069300404688";
			//String url = "https://api.numerousapp.com/v1/users/2088622069300404688/metrics";
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
			con.setRequestProperty("Authorization", "Basic " + owner);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestMethod("POST");

			con.setDoOutput(true);

			JSONObject jobj = new JSONObject();
			jobj.put("value", value);
			//jobj.put("description", "cool it works - again");
			//jobj.put("units", "kgs");

			System.out.println(jobj.toJSONString());
			//System.out.println(jobj.toString());

			//System.exit(0);

			String data = jobj.toJSONString();

			//String data =  "{\"value\":\"99\"}";
			OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
			out.write(data);
			out.close();

			// new InputStreamReader(con.getInputStream());

			BufferedReader in = new BufferedReader(
					new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			System.out.println(response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}


	}
	public static void updateDescription (String owner, String appkey,String value) {
		try {

			//String url = "https://api.numerousapp.com/v1/metrics/" + appkey + "/events";
			String url = "https://api.numerousapp.com/v1/metrics/2088622069300404688";
			//String url = "https://api.numerousapp.com/v1/users/2088622069300404688/metrics";
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
			con.setRequestProperty("Authorization", "Basic " + owner);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestMethod("PUT");

			con.setDoOutput(true);


			JSONObject jobj = new JSONObject();
			//jobj.put("value", value);
			jobj.put("description", value);
			//jobj.put("units", "kgs");

			System.out.println(jobj.toJSONString());
			//System.out.println(jobj.toString());

			//System.exit(0);

			String data = jobj.toJSONString();

			//String data =  "{\"value\":\"99\"}";
			OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
			out.write(data);
			out.close();

			// new InputStreamReader(con.getInputStream());

			BufferedReader in = new BufferedReader(
					new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			System.out.println(response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}


	}
	public static JSONObject getJsonObject(String json){
		JSONParser parser = new JSONParser();
		Object obj1 = null;
		try {
			obj1 = parser.parse(json);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			obj1 = parser.parse(json);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (JSONObject) obj1;
	}
}